/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "RenderManager.h"
#include <iostream>

#include "../kernel.h"
extern Kernel* g_kernel;

RenderManager::RenderManager(void)
{
    g_kernel->LPrintf("Creating a rendermanager...");
}

/** Initialize the backend */
int RenderManager::Init()
{
    // init the renderer
    if ( renderer.Init() ) {
        g_kernel->LPrintf("Error Initializing Renderer!");
    }
    // Add the current camera
    AddCamera ( renderer.GetCamera() );



    ICamera* camera = new Camera;
    camera->SetViewPort( Rect( 20, 20, 150, 150 ) );
    AddCamera ( camera );


    // activate
    SetActive( true );

    // Print success
    g_kernel->LPrintf("Renderer Initialized!");
    return 0;
}

/** Update active game states */
void RenderManager::Update(long dt)
{
    std::vector<ICamera*>::iterator camIter = camList.begin();
    //dt /= 1000.0f;
    for(; camIter != camList.end(); ++camIter )
    {
        ICamera* cam = (*camIter);
        if ( cam->IsActivated() )
            cam->Update( dt/1000.0f );
    }
    // Render the current scene
    renderer.PreRender();
    Render(dt);
    renderer.PostRender();
}

/** Close the backend */
void RenderManager::Shutdown()
{
    renderer.Shutdown();
    SetActive( false );
}


/** Draw all the gamestate views */
void RenderManager::Render(long dt)
{
   
    /**
     * Draw each StateView
     */
    type_StateView::iterator it = stateList.begin();
    for (; it != stateList.end(); it++ )
    {
        if ( (*it)->IsActive() )
        {
            std::vector<ICamera*>::iterator camIter = camList.begin();
            for(; camIter != camList.end(); ++camIter )
            {
                if ( (*camIter)->IsActivated() )
                    (*it)->Render( &renderer, *camIter );
            }
        }
    }
    gui::GUI_Manager::GetInstance()->PumpInput(); // temp
    gui::GUI_Manager::GetInstance()->Update( g_kernel->GetFrameTime() );
    gui::GUI_Manager::GetInstance()->Render( &renderer );

}

RenderManager::~RenderManager(void)
{
    //g_kernel->LPrintf("Closing RenderManager...");
}
